package cn.edu.cdu.argorithm.impl;

import cn.edu.cdu.argorithm.IValidParentheses;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;

public class ValidParenthesesSolutions implements IValidParentheses {
    @Override
    public boolean isValid(String s) {
        int n = s.length();
        if(n == 1) {
            return false;
        }
        Map<Character, Character> symbols = Map.of('(',')','{','}','[',']');
        Deque<Character> stack = new LinkedList<>();
        for(int i = 0; i < n; i++) {
            Character ch = s.charAt(i);
            if(symbols.containsKey(ch)) {
                stack.push(ch);
            } else {
                if(stack.isEmpty() || !Objects.equals(ch, symbols.get(stack.peek()))) {
                    return false;
                }
                stack.pop();
            }
        }
        return stack.isEmpty();
    }
}
